package com.zy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zy.common.pojo.EUDataGridResult;
import com.zy.common.pojo.TreeNode;
import com.zy.common.util.HttpClientUtil;
import com.zy.common.util.IDUtils;
import com.zy.common.util.TaotaoResult;
import com.zy.mapper.TbItemCatMapper;
import com.zy.mapper.TbItemDescMapper;
import com.zy.mapper.TbItemMapper;
import com.zy.pojo.TbItem;
import com.zy.pojo.TbItemCat;
import com.zy.pojo.TbItemCatExample;
import com.zy.pojo.TbItemExample;
import com.zy.pojo.TbItemExample.Criteria;
import com.zy.pojo.TbItemDesc;
import com.zy.pojo.TbItemDescExample;
import com.zy.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper itemMapper;

	@Autowired
	private TbItemDescMapper itemDescMapper;

	@Autowired
	private TbItemCatMapper tbItemCatMapper;
	
	@Value("${SEARCH_BASE_URL}")
	private String SEARCH_BASE_URL;
	@Value("${SEARCH_CONTENT_URL}")
	private String SEARCH_CONTENT_URL;

	@Override
	public TbItem getItemById(long itemId) {
		// TbItem item = itemMapper.selectByPrimaryKey(itemId);
		// 添加查询条件
		TbItemExample example = new TbItemExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andIdEqualTo(itemId);
		List<TbItem> list = itemMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			TbItem item = list.get(0);
			return item;
		}
		return null;
	}

	/**
	 * 分页查询商品信息
	 */
	@Override
	public EUDataGridResult getItemList(int page, int rows) {
		// 查询商品列表
		TbItemExample tbItemExample = new TbItemExample();
		PageHelper.startPage(page, rows);
		List<TbItem> list = itemMapper.selectByExample(tbItemExample);
		// 创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		// 取记录总条数
		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	/**
	 * 添加商品信息
	 */
	@Override
	public TaotaoResult addItem(TbItem item, String desc) throws Exception {
		// 需要补全商品id
		long itemId = IDUtils.genItemId();
		item.setId(itemId);
		// 商品状态 1正常 2下架 3 删除
		item.setStatus((byte) 1);
		item.setCreated(new Date());
		item.setUpdated(new Date());
		itemMapper.insert(item);
		//http://localhost:8083/search/manager/importall
		HttpClientUtil.doGet(SEARCH_BASE_URL+SEARCH_CONTENT_URL);
		// 添加商品描述信息
		TaotaoResult result = InsertItmeDesc(itemId, desc);
//		return result; // state--> 200 msg--> ok data-->null
		return TaotaoResult.ok();
	}

	/**
	 * 插入到描述信息表
	 */
	public TaotaoResult InsertItmeDesc(Long itemId, String desc) {
		// 补充未接受的数据
		TbItemDesc itemDesc=new TbItemDesc();
		itemDesc.setItemId(itemId);
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(new Date());
		itemDesc.setUpdated(new Date());
		itemDescMapper.insert(itemDesc);
		//http://localhost:8083/search/manager/importall
		HttpClientUtil.doGet(SEARCH_BASE_URL+SEARCH_CONTENT_URL);
		return TaotaoResult.ok(); // state--> 200 msg--> ok data-->null
	}

	/**
	 * 更新
	 */
	@Override
	public TaotaoResult updateItem(TbItem tbItem) {
		TbItemExample tbItemExample = new TbItemExample();
		Criteria createCriteria = tbItemExample.createCriteria();
		createCriteria.andIdEqualTo(tbItem.getId());
		tbItem.setUpdated(new Date());
		tbItem.setStatus((byte)1);
		itemMapper.updateByExample(tbItem, tbItemExample);
		//http://localhost:8083/search/manager/importall
		HttpClientUtil.doGet(SEARCH_BASE_URL+SEARCH_CONTENT_URL);
		return TaotaoResult.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public TaotaoResult deleteItem(long id) {
		itemMapper.deleteByPrimaryKey(id);
		//http://localhost:8083/search/manager/importall
		HttpClientUtil.doGet(SEARCH_BASE_URL+SEARCH_CONTENT_URL);
		return TaotaoResult.ok();
	}
}
